package com.huike.game.Activity;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.huike.game.R;
import com.huike.game.api.Api;
import com.huike.game.dao.EnvironmentDao;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Timer;
import java.util.TimerTask;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;

public class GameActivity13 extends AppCompatActivity {

    private EnvironmentDao dao;
    private ImageView iv_left_image;
    private TextView tv_left_text;
    private TextView tv_content_title;
    private ImageView iv_rigth_image;
    private TextView tv_right_text;
    private EditText et_game13_tem;
    private EditText et_game13_hum;
    private EditText et_game13_light;
    private EditText et_game13_cq;
    private EditText et_game13_pm;
    private EditText et_game13_way;
    private Switch sh_game13;
    private RequestQueue queue;

    private Boolean isSh = true;
    private NotificationManager manager;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game13);
        manager = (NotificationManager) this.getSystemService(this.NOTIFICATION_SERVICE);
        initView();


        SharedPreferences yuvalue = getSharedPreferences("yuvalue", MODE_PRIVATE);
        final String tem = yuvalue.getString("tem", null);
        final String hum = yuvalue.getString("hum", null);
        final String light = yuvalue.getString("light", null);
        final String cq = yuvalue.getString("co", null);
        final String pm = yuvalue.getString("pm", null);
        final String wy = yuvalue.getString("wy", null);

        if(tem == null || hum == null || light == null || cq ==null || pm == null || wy == null){

//            sh_game13.setChecked(false);
            et_game13_tem.setEnabled(true);
            et_game13_cq.setEnabled(true);
            et_game13_hum.setEnabled(true);
            et_game13_light.setEnabled(true);
            et_game13_way.setEnabled(true);
            et_game13_light.setEnabled(true);
            et_game13_pm.setEnabled(true);

        }else{

            Timer timer = new Timer();
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                initDate(tem,hum,light,cq,pm,wy);
                }
            },0,10000);

            et_game13_pm.setText(pm);
            et_game13_cq.setText(cq);
            et_game13_light.setText(light);
            et_game13_hum.setText(hum);
            et_game13_tem.setText(tem);
            et_game13_way.setText(wy);


            et_game13_tem.setEnabled(false);
            et_game13_cq.setEnabled(false);
            et_game13_hum.setEnabled(false);
            et_game13_light.setEnabled(false);
            et_game13_way.setEnabled(false);
            et_game13_light.setEnabled(false);
            et_game13_pm.setEnabled(false);

        }










        sh_game13.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               isSh = !isSh;
               sh_game13.setChecked(isSh);
                if(isSh == false){
                    et_game13_tem.setEnabled(true);
                    et_game13_cq.setEnabled(true);
                    et_game13_hum.setEnabled(true);
                    et_game13_light.setEnabled(true);
                    et_game13_way.setEnabled(true);
                    et_game13_light.setEnabled(true);
                    et_game13_pm.setEnabled(true);
                }else{
                    et_game13_tem.setEnabled(false);
                    et_game13_cq.setEnabled(false);
                    et_game13_hum.setEnabled(false);
                    et_game13_light.setEnabled(false);
                    et_game13_way.setEnabled(false);
                    et_game13_light.setEnabled(false);
                    et_game13_pm.setEnabled(false);
                }

            }
        });




    }

    private void initDate(final String tem, final String hum, String light, final String cq, final String pm, final String wy) {

        queue = Volley.newRequestQueue(this);

        JsonObjectRequest request = new JsonObjectRequest(1, Api.api + "environmental", new Response.Listener<JSONObject>() {
            @SuppressLint("NewApi")
            @Override
            public void onResponse(JSONObject jsonObject) {
                try {
                    String tems = jsonObject.getString("data");
                    JSONObject object = new JSONObject(tems);
                    String tem1 = object.getString("tem");
                    String hum1 = object.getString("hum");
                    String cq1 = object.getString("cq2");
                    String PM1 = object.getString("PM2.5");
                    String way1 = object.getString("way");

                    Log.e("---",tems);

//                    8.0之前的写法
//                    Notification.Builder builder = new Notification.Builder(GameActivity13.this);
//                    builder.setContentTitle("阈值通知");
//                    builder.setContentText("123");
//                    builder.setSmallIcon(R.mipmap.ic_launcher);
//                    builder.setLargeIcon(BitmapFactory.decodeResource(GameActivity13.this.getResources(),R.mipmap.ic_launcher));
//                    manager.notify(1,builder.build());

//                    8.0后面
                    //创建notification

//适配安卓8.0的消息渠道


//                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//                        /**
//                         * Oreo不用Priority了，用importance
//                         * IMPORTANCE_NONE 关闭通知
//                         * IMPORTANCE_MIN 开启通知，不会弹出，但没有提示音，状态栏中无显示
//                         * IMPORTANCE_LOW 开启通知，不会弹出，不发出提示音，状态栏中显示
//                         * IMPORTANCE_DEFAULT 开启通知，不会弹出，发出提示音，状态栏中显示
//                         * IMPORTANCE_HIGH 开启通知，会弹出，发出提示音，状态栏中显示
//                         */
//                        NotificationChannel channel = new NotificationChannel(channelID, channelName, NotificationManager.IMPORTANCE_LOW);
//                        manager.createNotificationChannel(channel);
//                    }
//                    Notification  notification1 = new NotificationCompat.Builder(GameActivity13.this)
//                            .setTicker("正在下载") //通知首次出现在通知栏，带上升动画效果的
//                            .setOngoing(true)//设置他为一个正在进行的通知。他们通常是用来表示一个后台任务,用户积极参与(如播放音乐) 或以某种方式正在等待,因此占用设备(如一个文件下载,同步操作,主动网络连接)
//                            .setSmallIcon(R.mipmap.ic_launcher)//设置通知小ICON
//                            .setContentTitle("下载中")//设置通知栏标题
//                            .setContentText("正在下载请稍等")//设置通知栏显示内容
//                            .setChannelId("0")//*
//                            .build();
//                    manager.notify(1,notification1);


                    if(Integer.parseInt(tem1)  >  Integer.parseInt(tem)){
                        Toast.makeText(GameActivity13.this, "tem", Toast.LENGTH_SHORT).show();
                        Notification notification = new NotificationCompat
                            .Builder(GameActivity13.this,"channel")
                            .setContentText("当前值:" + tem +" 阈值:"+tem1)//*
                            .setContentTitle("阈值管理")//*
                            .setSmallIcon(R.mipmap.ic_launcher_round)//*
                            .setAutoCancel(true)//自动消失
                            .setChannelId("0")
//                            .setLargeIcon(BitmapFactory.decodeResource(GameActivity13.this.getResources(),R.mipmap.ic_launcher))
                            .build();
                            manager.notify(1,notification);

                    }
                    if(Integer.parseInt(hum1)  >  Integer.parseInt(hum)){
                        Toast.makeText(GameActivity13.this, "hum", Toast.LENGTH_SHORT).show();

                        Notification builder = new NotificationCompat.Builder(GameActivity13.this, "channel")
                                .setContentTitle("阈值管理")
                                .setContentText("当前值:" + hum +" 阈值:"+hum1)
                                .setAutoCancel(true)
                                .setSmallIcon(R.mipmap.ic_launcher_round)
                                .setChannelId("0")
                                .build();
                        manager.notify(1,builder);

                    }
                    if(Integer.parseInt(cq1)  >  Integer.parseInt(cq)){
                        Toast.makeText(GameActivity13.this, "cq", Toast.LENGTH_SHORT).show();
                        Notification builder = new NotificationCompat.Builder(GameActivity13.this, "channel")
                                .setContentTitle("阈值管理")
                                .setContentText("当前值:" + cq +" 阈值:"+cq1)
                                .setAutoCancel(true)
                                .setSmallIcon(R.mipmap.ic_launcher_round)
                                .setChannelId("0")
                                .build();
                        manager.notify(3,builder);

                    }
                    if(Integer.parseInt(PM1)  >  Integer.parseInt(pm)){
                        Toast.makeText(GameActivity13.this, "PM1", Toast.LENGTH_SHORT).show();
                        Notification builder = new NotificationCompat.Builder(GameActivity13.this, "channel")
                                .setContentTitle("阈值管理")
                                .setContentText("当前值:" + pm +" 阈值:"+PM1)
                                .setAutoCancel(true)
                                .setSmallIcon(R.mipmap.ic_launcher_round)
                                .setChannelId("0")
                                .build();
                        manager.notify(4,builder);


                    }
                    if(Integer.parseInt(way1)  >  Integer.parseInt(wy)){
                        Toast.makeText(GameActivity13.this, "way", Toast.LENGTH_SHORT).show();
                        Notification builder = new NotificationCompat.Builder(GameActivity13.this, "channel")
                                .setContentTitle("阈值管理")
                                .setContentText("当前值:" + wy +" 阈值:"+way1)
                                .setAutoCancel(true)
                                .setSmallIcon(R.mipmap.ic_launcher_round)
                                .setChannelId("0")
                                .build();
                        manager.notify(5,builder);


                    }



                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.e("-----",volleyError.getMessage());
            }
        });

        queue.add(request);


    }

    //    点击按钮保存数据
    public void saveValue(View v){
//        获取阈值
        String tem = et_game13_tem.getText().toString();
        String hum = et_game13_hum.getText().toString();
        String light = et_game13_light.getText().toString();
        String co = et_game13_cq.getText().toString();
        String pm = et_game13_pm.getText().toString();
        String way = et_game13_way.getText().toString();

//        添加缓存
        SharedPreferences yuvalue = getSharedPreferences("yuvalue", MODE_PRIVATE);
        yuvalue.edit().putString("tem",tem).putString("hum",hum).putString("light",light).putString("co",co).putString("pm",pm).putString("wy",way).commit();
        Toast.makeText(this, "保存成功！", Toast.LENGTH_SHORT).show();

    }

    private void initView() {
        iv_left_image = (ImageView) findViewById(R.id.iv_left_image);
        tv_left_text = (TextView) findViewById(R.id.tv_left_text);
        tv_content_title = (TextView) findViewById(R.id.tv_content_title);
        iv_rigth_image = (ImageView) findViewById(R.id.iv_rigth_image);
        tv_right_text = (TextView) findViewById(R.id.tv_right_text);
        sh_game13 = findViewById(R.id.sh_game13);

        iv_rigth_image.setVisibility(View.GONE);
        tv_right_text.setVisibility(View.GONE);

        tv_content_title.setText("阈值设置");

        iv_left_image.setImageResource(R.drawable.tiao);
        tv_left_text.setVisibility(View.GONE);
        et_game13_tem = (EditText) findViewById(R.id.et_game13_tem);
        et_game13_hum = (EditText) findViewById(R.id.et_game13_hum);
        et_game13_light = (EditText) findViewById(R.id.et_game13_light);
        et_game13_cq = (EditText) findViewById(R.id.et_game13_cq);
        et_game13_pm = (EditText) findViewById(R.id.et_game13_pm);
        et_game13_way = (EditText) findViewById(R.id.et_game13_way);
    }

}
